<template>
    <div style="margin-top: 100px;">
        <van-row style="margin-bottom: 50px;">
            <van-col :span="6"></van-col>
            <van-col :span="12">
                <h1>elmSSMAPP</h1>
            </van-col>
            <van-col :span="6"></van-col>
        </van-row>
        <van-row style="margin-bottom: 50px;">
            <van-col :span="7"></van-col>
            <van-col :span="8">
                <img src="../assets/yellow.png" style="width: 100px;height: 100px;"/>
            </van-col>
            <van-col>
                <div style="margin-top: 70px;">
                    <img src="../assets/bb.png" style="width: 20px;height: 20px;"/>
                </div>
            </van-col>
            <van-col :span="7"></van-col>
        </van-row>
        <van-row>
            <van-col :span="24">
                <van-form @submit="onSubmit">
                    <van-cell-group inset>
                        <van-field
                        v-model="userName"
                        name="userName"
                        label="用户名"
                        placeholder="用户名"
                        :rules="[{ required: true, message: '请填写用户名' }]"
                        />
                        <van-field
                        v-model="password"
                        type="password"
                        name="password"
                        label="密码"
                        placeholder="密码"
                        :rules="[{ required: true, message: '请填写密码' }]"
                        />
                    </van-cell-group>
                    <div style="margin: 16px;">
                        <van-button round block type="primary" native-type="submit">
                            提交
                        </van-button>
                    </div>
                    </van-form>
            </van-col>
        </van-row>
    </div>
    
</template>

<script setup>
import { ref,getCurrentInstance } from "vue";
import {showNotify} from 'vant';

const {proxy} = getCurrentInstance();

const userName = ref('');
const password = ref('');

const onSubmit = (values) => {
    console.log('submit', values);// {userName:'',password:''}
    proxy.$axios({
        method: 'post',
        url: '/login',
        data: values
    }).then(res=>{
        let resultBean = res.data;
        if(resultBean.code == 200){
           let user = resultBean.data;
           //将登录成功的用户存储到本地存储localStorage
           window.localStorage.setItem('loginUser',JSON.stringify(user));
           proxy.$router.push("/main");
        }else{
            showNotify({message: resultBean.msg});
        }
    }).catch(error=>{
        showNotify({message: error.message});
    })

};


</script>

<style>
</style>